jQuery().ready(
			function(){
				
jQuery("#list1").jqGrid({
							url : baserUrl+'device/list?'+$('form').serialize(),
							height: '100%',
							datatype : "json",
							colNames : [ 'ID','状态','设备标识', '主叫号码','设备类型','厂商名称','固件版本','管理域','用户地址','设备模式'],
							colModel : [{
								name : 'id',
								index : 'id',
								width : 75,
								editable:true,
								hidden:true,
								editrules:{required:false},
								align:'center',
							}, {
								name : 'status',
								index : 'status',
								width : 75,
								editable:false,
								editrules:{required:false},
								formatter:formatStatus,
								align:'center',
							},{
								name : 'imsi',
								index : 'imsi',
								width : 100,
								editable:true,
								align:'center',
							},{
								name : 'mdn',
								index : 'mdn',
								width : 100,
								editable:true,
								align:'center',
							
							},{
								name : 'guiDevicetype.name',
								index : 'guiDevicetype.name',
								width : 100,
								sortable:true,
								editable:true,
								align:'center',
							},{
								name : 'devManufac',
								index : 'devManufac',
								width : 100,
								sortable:true,
								editable:true,
								align:'center',
							}, {
								name : 'softVer',
								index : 'softVer',
								width : 100,
								editable:true,
							}, {
								name : 'fullPath',
								index : 'fullPath',
								width : 100,
								editable:true,
								align:'center',
							}, {
								name : 'subscriberAddress',
								index : 'subscriberAddress',
								width : 100,
								editable:true,
							}, {
								name : 'accessMode',
								index : 'accessMode',
								width : 100,
								editable:true,
								align:'center',
							}],
							rowNum : 20,//每页显示条数
							autowidth : true,
							rowList : [20, 50, 100],
							pager : jQuery('#pager1'),
							sortname : 'id',
							viewrecords : true,//是否显示行数
							sortorder : "desc",
							multiselect: true,
							cellEdit: false,
							caption : "设备列表",
							cellsubmit: 'clientArray',
							cellurl:baserUrl+'device/save',
							editurl:baserUrl+'device/save',
							jsonReader : {  
	    						root: "rows",   // json中代表实际模型数据的入口  
	    						page: "currentPage",   // json中代表当前页码的数据  
	    						total: "totalPage", // json中代表页码总数的数据  
	    						records: "total", // json中代表数据行总数的数据  
	    						repeatitems: false, // 如果设为false，则jqGrid在解析json时，会根据name来搜索对应的数据元素（即可以json中元素可以不按顺序）；而所使用的name是来自于colModel中的name设定。  
	    						cell: "cell",  
	    						id: "0",  
	    						userdata: "userdata",
    						},
    						    prmNames : {
							       
							    }  
						});
						
						jQuery("#list1").navGrid('#pager1',{edit:false,add:false,del:false,search:false},
							{  
							
								
						    },
						    {
						    	beforeSubmit : function(postdata, formid) 
						    	{
							    	$.each(postdata,function(key, value){
							    		postdata['guiDevice.'+key] = value;
							    		delete postdata[key];
									});
		
					               	console.info(postdata);
							    	return [true,""]
								},
								afterShowForm: function($form) {
                         		 $form.closest('div.ui-jqdialog').center();
                      			},
                      			closeAfterAdd: true,
    						    closeAfterEdit: true
						    },
						    {
						    	reloadAfterSubmit:true,
						    	url:baserUrl+'device/delete',
								afterShowForm: function($form) {
                         		 $form.closest('div.ui-jqdialog').center();
                      			}
						    },//del
						    {}
						    ).navButtonAdd('#pager1'
							,{
								caption:"", buttonicon:"ui-icon-search",   
								onClickButton: function(){searchevent();}, position:"first"  
						    }
							
						)
						/*.navButtonAdd('#pager1'
							,{
								caption:"", buttonicon:"ui-icon-pencil",   
								onClickButton: function(){editevent();}, position:"first"  
						    }
							
						)*/
						
						.navButtonAdd('#pager1'
							,{
								caption:"", buttonicon:"ui-icon-plus",   
								onClickButton: function(){getDetail(baserUrl+"device/detail");}, position:"first"  
						    }
							
						).navButtonAdd('#pager1'
							,{
								caption:"", buttonicon:"ui-icon-pencil",   
								onClickButton: function(){editevent();}, position:"first"  
						    }
							
						);
						
				/**jQuery("#list1").jqGrid('navGrid','#pager1',{  
       			 		searchfunc : searchevent,
    					});**/
    					
    					initdialog();
    					initaddDialog();
			});

function formatStatus(cellvalue){ 
	if(cellvalue == 0){
		return "未知";
	}else if(cellvalue == 1){
		return "已开户";
	}else if(cellvalue == 2){
		return "已激活";
	}else if(cellvalue == 3){
		return "未激活";
	}
}

function initdialog (){
	$( ".searchbar" ).dialog({
autoOpen: false,
height: 250,
width: 400,
modal: true,
buttons: {
"查询": function() {
queryDevice();
},
"取消": function() {
$( this ).dialog( "close" );
}
},
close: function() {

}
});
}

function initaddDialog (){
	$( ".deviceDetail" )
	.dialog({
autoOpen: false,
height: 280,
width: 500,
modal: true,
buttons: {
"确认": function() {
		submitData();
},
"取消": function() {
	$( ".deviceDetail" ).dialog( "close" );
}
},
close: function() {
}
});
}

function reload(){
	$( ".deviceDetail" ).dialog( "close" );
	$("#list1").trigger("reloadGrid");
}


var  editevent = function(){
	var id = $("#list1").jqGrid('getGridParam','selrow');
    //根据id获取行数据,返回的是列表
    var rowDatas = $("#list1").jqGrid('getRowData', id);
    //取到选中行某一字段的值，其中name为colModel中定义的字段名
    var rowId = rowDatas["id"];
	getDetail(baserUrl+'device/detail?id='+rowId);
}

var  searchevent = function(){
	$( ".searchbar" ).dialog( "open" );
}
var  getDetail= function(url){
	$('.deviceDetail').load(url);
	$( ".deviceDetail" ).dialog( "open" );
	//$( ".userDetail" ).html('<iframe id="userDetailFrame" style="border: 0px; " src="' + url + '" width="100%" height="100%"></iframe>');
    //$( ".userDetail" ).dialog( "open" );
}


